﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinancialManagement.Models.Repository
{
    public class AccountRepository
    {
        internal bool IsUserExist(string userID, string password)
        {
            bool isExist = false;
            using (var dc = new FinancialMGTDataContext())
            {
                var query = dc.T_User.Where(o => o.UserID == userID && o.Password == password);
                if (query != null && query.Count() > 0)
                {
                    isExist = true;
                }
            }
            return isExist;
        }


        internal bool ChangePassword(string userID, ChangePasswordModel model)
        {
            bool isOk = true;
            try
            {
                using (var dc = new FinancialMGTDataContext())
                {
                    var tModel = dc.T_User.FirstOrDefault(o => o.UserID.Equals(userID));
                    if (tModel != null)
                    {
                        tModel.Password = model.NewPassword;
                        dc.SubmitChanges();
                    }
                    else
                    {
                        isOk = false;
                    }
                }
            }
            catch (Exception)
            {
                isOk = false;
            }
            return isOk;
        }

        internal string GetDepIdByUserID(string userID)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var dep = (from u in dc.T_User
                           where u.UserID.Equals(userID)
                           select u.DepartmentID).FirstOrDefault();
                return dep;
            }
        }

        internal string GetDepNameByUserID(string userID)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var dep = (from u in dc.T_User
                           where u.UserID.Equals(userID)
                           select u.DepartmentName).FirstOrDefault();
                return dep;
            }
        }

        /// <summary>
        /// 根据工号判定是否为财务人员
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        internal bool GetRoleAdminByUserID(string userID)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var roleId = (from u in dc.T_User
                              where u.UserID.Equals(userID)
                              select u.RoleID).FirstOrDefault();
                if (Utils.Util.ADMINROLE.Equals(roleId))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        internal string GetUserNameByUserID(string userID)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                return (from u in dc.T_User
                        where u.UserID.Equals(userID)
                        select u.UserName).FirstOrDefault();

            }
        }
    }
}